package com.splant.android.smart.UILevel.Community;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.loopj.android.http.RequestParams;
import com.splant.android.R;
import com.splant.android.smart.CommunicationLevel.sPlantAsyncHttpClient;
import com.splant.android.smart.CommunicationLevel.sPlantJsonHttpResponseHandler;
import com.splant.android.smart.ModelLevel.TimerWater;
import com.splant.android.smart.ModelLevel.sPlantRequestParams;
import com.splant.android.smart.SystemDefinedLevel.DeciveManager.DeviveMessages;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.DateTimeSelceted.DateTimeSet;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.DateTimeSelceted.SlideDateTimeListener;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.DateTimeSelceted.SlideDateTimePicker;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.WheelView.WheelView;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Garden_TimerWaterEdit_Activity extends FragmentActivity implements View.OnClickListener {


    private Button btn_goBack;

    private TextView tv_waterSwitchNo;
    private TextView tv_startTime;
    private TextView tv_waterLength;
    private Button btn_save;
    private Button btn_cancel;

    private TextView tv_month1;
    private TextView tv_month2;
    private TextView tv_month3;
    private TextView tv_month4;
    private TextView tv_month5;
    private TextView tv_month6;
    private TextView tv_month7;
    private TextView tv_month8;
    private TextView tv_month9;
    private TextView tv_month10;
    private TextView tv_month11;
    private TextView tv_month12;
    private TextView tv_month13;
    private TextView tv_month14;
    private TextView tv_month15;
    private TextView tv_month16;
    private TextView tv_month17;
    private TextView tv_month18;
    private TextView tv_month19;
    private TextView tv_month20;
    private TextView tv_month21;
    private TextView tv_month22;
    private TextView tv_month23;
    private TextView tv_month24;
    private TextView tv_month25;
    private TextView tv_month26;
    private TextView tv_month27;
    private TextView tv_month28;
    private TextView tv_month29;
    private TextView tv_month30;
    private TextView tv_month31;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.garden_timerwatersedit_activity);

        btn_goBack = (Button) findViewById(R.id.btn_goBack);
        tv_waterSwitchNo = (TextView) findViewById(R.id.tv_waterSwitchNo);
        tv_startTime = (TextView) findViewById(R.id.tv_startTime);
        tv_waterLength = (TextView) findViewById(R.id.tv_waterLength);
        btn_save = (Button) findViewById(R.id.btn_save);
        btn_cancel = (Button) findViewById(R.id.btn_cancel);

        tv_month1 = (TextView) findViewById(R.id.tv_month1);
        tv_month2 = (TextView) findViewById(R.id.tv_month2);
        tv_month3 = (TextView) findViewById(R.id.tv_month3);
        tv_month4 = (TextView) findViewById(R.id.tv_month4);
        tv_month5 = (TextView) findViewById(R.id.tv_month5);
        tv_month6 = (TextView) findViewById(R.id.tv_month6);
        tv_month7 = (TextView) findViewById(R.id.tv_month7);
        tv_month8 = (TextView) findViewById(R.id.tv_month8);
        tv_month9 = (TextView) findViewById(R.id.tv_month9);
        tv_month10 = (TextView) findViewById(R.id.tv_month10);
        tv_month11 = (TextView) findViewById(R.id.tv_month11);
        tv_month12 = (TextView) findViewById(R.id.tv_month12);
        tv_month13 = (TextView) findViewById(R.id.tv_month13);
        tv_month14 = (TextView) findViewById(R.id.tv_month14);
        tv_month15 = (TextView) findViewById(R.id.tv_month15);
        tv_month16 = (TextView) findViewById(R.id.tv_month16);
        tv_month17 = (TextView) findViewById(R.id.tv_month17);
        tv_month18 = (TextView) findViewById(R.id.tv_month18);
        tv_month19 = (TextView) findViewById(R.id.tv_month19);
        tv_month20 = (TextView) findViewById(R.id.tv_month20);
        tv_month21 = (TextView) findViewById(R.id.tv_month21);
        tv_month22 = (TextView) findViewById(R.id.tv_month22);
        tv_month23 = (TextView) findViewById(R.id.tv_month23);
        tv_month24 = (TextView) findViewById(R.id.tv_month24);
        tv_month25 = (TextView) findViewById(R.id.tv_month25);
        tv_month26 = (TextView) findViewById(R.id.tv_month26);
        tv_month27 = (TextView) findViewById(R.id.tv_month27);
        tv_month28 = (TextView) findViewById(R.id.tv_month28);
        tv_month29 = (TextView) findViewById(R.id.tv_month29);
        tv_month30 = (TextView) findViewById(R.id.tv_month30);
        tv_month31 = (TextView) findViewById(R.id.tv_month31);

        init();

        btn_goBack.setOnClickListener(this);
        tv_month1.setOnClickListener(this);
        tv_month2.setOnClickListener(this);
        tv_month3.setOnClickListener(this);
        tv_month4.setOnClickListener(this);
        tv_month5.setOnClickListener(this);
        tv_month6.setOnClickListener(this);
        tv_month7.setOnClickListener(this);
        tv_month8.setOnClickListener(this);
        tv_month9.setOnClickListener(this);
        tv_month10.setOnClickListener(this);
        tv_month11.setOnClickListener(this);
        tv_month12.setOnClickListener(this);
        tv_month13.setOnClickListener(this);
        tv_month14.setOnClickListener(this);
        tv_month15.setOnClickListener(this);
        tv_month16.setOnClickListener(this);
        tv_month17.setOnClickListener(this);
        tv_month18.setOnClickListener(this);
        tv_month19.setOnClickListener(this);
        tv_month20.setOnClickListener(this);
        tv_month21.setOnClickListener(this);
        tv_month22.setOnClickListener(this);
        tv_month23.setOnClickListener(this);
        tv_month24.setOnClickListener(this);
        tv_month25.setOnClickListener(this);
        tv_month26.setOnClickListener(this);
        tv_month27.setOnClickListener(this);
        tv_month28.setOnClickListener(this);
        tv_month29.setOnClickListener(this);
        tv_month30.setOnClickListener(this);
        tv_month31.setOnClickListener(this);

        btn_save.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
        tv_startTime.setOnClickListener(this);
        tv_waterLength.setOnClickListener(this);
        btn_goBack.setOnClickListener(this);
    }

    private TimerWater timerWater;
    private void init() {

        timerWater = (TimerWater) getIntent().getSerializableExtra("timerWater");

        if (timerWater==null)
            return;

        tv_waterSwitchNo.setText(timerWater.getWaterName() +" 开关" + timerWater.getSwitchNo());
        tv_startTime.setText(timerWater.getStartTime());
        tv_waterLength.setText(String.valueOf(timerWater.getWaterLength())+"分钟");
        timerCycle = timerWater.getWaterCycle();
        startTime=timerWater.getStartTime();

        isTimeDays=new boolean[31];

        showTimerCycle();

    }
    private String timerCycle="";
    private String startTime="";

    private boolean[] isTimeDays;
    private String[] daysCycle;
    private void showTimerCycle() {

        daysCycle=timerCycle.split(",");
        if (daysCycle!=null)
        {
            for (String d:daysCycle)
            {
                int day=Integer.parseInt(d);
                isTimeDays[day-1]=true;
            }
        }
        else
            return;

        if (isTimeDays[0]) {
            tv_month1.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[1]) {
            tv_month2.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[2]) {
            tv_month3.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[3]) {
            tv_month4.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[4]) {
            tv_month5.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[5]) {
            tv_month6.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[6]) {
            tv_month7.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[7]) {
            tv_month8.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[8]) {
            tv_month9.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[9]) {
            tv_month10.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[10]) {
            tv_month11.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[11]) {
            tv_month12.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[12]) {
            tv_month13.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[13]) {
            tv_month14.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[14]) {
            tv_month15.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[15]) {
            tv_month16.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[16]) {
            tv_month17.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[17]) {
            tv_month18.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[18]) {
            tv_month19.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[19]) {
            tv_month20.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[20]) {
            tv_month21.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[21]) {
            tv_month22.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[22]) {
            tv_month23.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[23]) {
            tv_month24.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }
        if (isTimeDays[24]) {
            tv_month25.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[25]) {
            tv_month26.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }
        if (isTimeDays[26]) {
            tv_month27.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[27]) {
            tv_month28.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[28]) {
            tv_month29.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[29]) {
            tv_month30.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }

        if (isTimeDays[30]) {
            tv_month31.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }
    }

    private sPlantAsyncHttpClient sPlantClient;

    private void getNewTimerCycle()
    {
        timerCycle="";
        if (isTimeDays[0]) {
            timerCycle+="1,";
        }

        if (isTimeDays[1]) {
            timerCycle+="2,";
        }

        if (isTimeDays[2]) {
            timerCycle+="3,";
        }

        if (isTimeDays[3]) {
            timerCycle+="4,";
        }

        if (isTimeDays[4]) {
            timerCycle+="5,";
        }

        if (isTimeDays[5]) {
            timerCycle+="6,";
        }

        if (isTimeDays[6]) {
            timerCycle+="7,";
        }

        if (isTimeDays[7]) {
            timerCycle+="8,";
        }

        if (isTimeDays[8]) {
            timerCycle+="9,";
        }

        if (isTimeDays[9]) {
            timerCycle+="10,";
        }

        if (isTimeDays[10]) {
            timerCycle+="11,";
        }

        if (isTimeDays[11]) {
            timerCycle+="12,";
        }

        if (isTimeDays[12]) {
            timerCycle+="13,";
        }

        if (isTimeDays[13]) {
            timerCycle+="14,";
        }

        if (isTimeDays[14]) {
            timerCycle+="15,";
        }

        if (isTimeDays[15]) {
            timerCycle+="16,";
        }

        if (isTimeDays[16]) {
            timerCycle+="17,";
        }

        if (isTimeDays[17]) {
            timerCycle+="18,";
        }

        if (isTimeDays[18]) {
            timerCycle+="19,";
        }

        if (isTimeDays[19]) {
            timerCycle+="20,";
        }

        if (isTimeDays[20]) {
            timerCycle+="21,";
        }

        if (isTimeDays[21]) {
            timerCycle+="22,";
        }

        if (isTimeDays[22]) {
            timerCycle+="23,";
        }

        if (isTimeDays[23]) {
            timerCycle+="24,";
        }
        if (isTimeDays[24]) {
            timerCycle+="25,";
        }

        if (isTimeDays[25]) {
            timerCycle+="26,";
        }
        if (isTimeDays[26]) {
            timerCycle+="27,";
        }

        if (isTimeDays[27]) {
            timerCycle+="28,";
        }

        if (isTimeDays[28]) {
            timerCycle+="29,";
        }

        if (isTimeDays[29]) {
            timerCycle+="30,";
        }

        if (isTimeDays[30]) {
            timerCycle+="31,";
        }

        if (timerCycle.length()>0)
        {
            timerCycle=timerCycle.substring(0,timerCycle.length()-1);
        }
    }

    private void save() {

        getNewTimerCycle();

        if (timerCycle.equals(""))
            return;

        sPlantClient = sPlantAsyncHttpClient.getInstance();

        sPlantRequestParams params = new sPlantRequestParams();
        JSONArray timerWaters=new JSONArray();
        try
        {
            JSONObject timerWaterJObj=new JSONObject();
            timerWaterJObj.put("recId", timerWater.getRecId());
            timerWaterJObj.put("waterId", timerWater.getWaterId());
            timerWaterJObj.put("waterName", timerWater.getWaterName());
            timerWaterJObj.put("switchNo", timerWater.getSwitchNo());
            timerWaterJObj.put("waterCycle", timerCycle);
            timerWaterJObj.put("startTime", startTime);
            timerWaterJObj.put("waterLength", timerWater.getWaterLength());
            timerWaterJObj.put("allowUsed", timerWater.getAllowUsed());
            timerWaters.put(timerWaterJObj);
            params.putParams("timerWaters",timerWaters);
        }
        catch (Exception e)
        {

        }
        sPlantClient.post(this,sPlantAsyncHttpClient.UPDATE_TIMERWATER, params, new sPlantJsonHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        try {

                            if (response.getInt("succeed") == 1) {

                                Toast.makeText(Garden_TimerWaterEdit_Activity.this, "保存成功！", Toast.LENGTH_SHORT).show();

                            } else if (response.getInt("succeed") == 0) {

                                Toast.makeText(Garden_TimerWaterEdit_Activity.this, "保存失败！", Toast.LENGTH_SHORT).show();

                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {

                        Toast.makeText(Garden_TimerWaterEdit_Activity.this, getString(R.string.system_request_error), Toast.LENGTH_SHORT).show();
                    }

                });
    }

    private AlertDialog waterTimeDialog;

    public List<String> getTimeData() {

        for (int i = 1; i <= 10; i++) {
            mDatas.add(i + " 分钟");
        }

        for (int i = 15; i <= 30; i+=5) {
            mDatas.add(i + " 分钟");
        }

        for (int i = 40; i <= 60; i+=10) {
            mDatas.add(i + " 分钟");
        }

        return mDatas;
    }

    private List<String> mDatas = new ArrayList<>();

    private void selectWaterLength()
    {
        View outerView = LayoutInflater.from(this).inflate(R.layout.system_unit_wheel_view, null);
        WheelView wv = (WheelView) outerView.findViewById(R.id.wheel_view_wv);
        wv.setOffset(2);
        wv.setItems(getTimeData());
        wv.setSeletion(9);

        wv.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                String[] items = item.split(" ");
                timerWater.setWaterLength(Integer.parseInt(items[0]));
            }
        });
        TextView tv = new TextView(this);
        tv.setText("请选择");
        tv.setTextSize(20);
        tv.setPadding(10, 10, 10, 10);
        tv.setGravity(Gravity.CENTER);
        tv.setTextColor(Color.parseColor("#ffaa66cc"));

        waterTimeDialog = new AlertDialog.Builder(this)
                .setCustomTitle(tv)
                .setView(outerView)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        tv_waterLength.setText(timerWater.getWaterLength()+"分钟");

                    }
                }).create();
        waterTimeDialog.show();

        WindowManager.LayoutParams params = waterTimeDialog.getWindow().getAttributes();
        params.width = (2 * DeviveMessages.getScreenInfo(this,getWindowManager()).getScreenWidth()) / 5;
        waterTimeDialog.getWindow().setAttributes(params);
    }

    private void  selectWaterStartTime()
    {
        try
        {
            DateTimeSet.dateTimeType = DateTimeSet.DateTimeType.TIME;
            new SlideDateTimePicker.Builder(getSupportFragmentManager())
                    .setListener(slideDateTimeListener)
                    .setInitialDate(new Date())
                    //.setMinDate(minDate)
                    //.setMaxDate(maxDate)
                    //.setIs24HourTime(true)
                    .setTheme(SlideDateTimePicker.HOLO_LIGHT)
                    // .setIndicatorColor(R.color.system_appBg)
                    .build()
                    .show();

        }
        catch (Exception e) {

        }
    }

    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

    private SlideDateTimeListener slideDateTimeListener = new SlideDateTimeListener() {

        @Override
        public void onDateTimeSet(Date date) {

            startTime=sdf.format(date);

            tv_startTime.setText(startTime);

        }
        @Override
        public void onDateTimeCancel() {

        }
    };

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_goBack:
                finish();
                break;
            case R.id.tv_startTime:
                selectWaterStartTime();
                break;
            case R.id.tv_waterLength:
                selectWaterLength();
                break;
            case R.id.btn_save:
                save();
                break;
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.tv_month1:
                selectCancelDay(1, tv_month1);
                break;
            case R.id.tv_month2:
                selectCancelDay(2, tv_month2);
                break;
            case R.id.tv_month3:
                selectCancelDay(3, tv_month3);
                break;
            case R.id.tv_month4:
                selectCancelDay(4, tv_month4);
                break;
            case R.id.tv_month5:
                selectCancelDay(5, tv_month5);
                break;
            case R.id.tv_month6:
                selectCancelDay(6, tv_month6);
                break;
            case R.id.tv_month7:
                selectCancelDay(7, tv_month7);
                break;
            case R.id.tv_month8:
                selectCancelDay(8, tv_month8);
                break;
            case R.id.tv_month9:
                selectCancelDay(9, tv_month9);
                break;
            case R.id.tv_month10:
                selectCancelDay(10, tv_month10);
                break;
            case R.id.tv_month11:
                selectCancelDay(11, tv_month11);
                break;
            case R.id.tv_month12:
                selectCancelDay(12, tv_month12);
                break;
            case R.id.tv_month13:
                selectCancelDay(13, tv_month13);
                break;
            case R.id.tv_month14:
                selectCancelDay(14, tv_month14);
                break;
            case R.id.tv_month15:
                selectCancelDay(15, tv_month15);
                break;
            case R.id.tv_month16:
                selectCancelDay(16, tv_month16);
                break;
            case R.id.tv_month17:
                selectCancelDay(17, tv_month17);
                break;
            case R.id.tv_month18:
                selectCancelDay(18, tv_month18);
                break;
            case R.id.tv_month19:
                selectCancelDay(19, tv_month19);
                break;
            case R.id.tv_month20:
                selectCancelDay(20, tv_month20);
                break;
            case R.id.tv_month21:
                selectCancelDay(21, tv_month21);
                break;
            case R.id.tv_month22:
                selectCancelDay(22, tv_month22);
                break;
            case R.id.tv_month23:
                selectCancelDay(23, tv_month23);
                break;
            case R.id.tv_month24:
                selectCancelDay(24, tv_month24);
                break;
            case R.id.tv_month25:
                selectCancelDay(25, tv_month25);
                break;
            case R.id.tv_month26:
                selectCancelDay(26, tv_month26);
                break;
            case R.id.tv_month27:
                selectCancelDay(27, tv_month27);
                break;
            case R.id.tv_month28:
                selectCancelDay(28, tv_month28);
                break;
            case R.id.tv_month29:
                selectCancelDay(29, tv_month29);
                break;
            case R.id.tv_month30:
                selectCancelDay(30, tv_month30);
                break;
            case R.id.tv_month31:
                selectCancelDay(31, tv_month31);
                break;
        }
    }

    public void selectCancelDay(int day, TextView tv_month) {

        if (isTimeDays[day-1])
        {
            tv_month.setBackgroundResource(R.color.garden_monthtable_bg_normal);
            isTimeDays[day-1]=false;
        }
        else
        {
            tv_month.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
            isTimeDays[day-1]=true;
        }

    }

}
